package com.tutor.lzm.function.tutor;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewStub;
import android.widget.AdapterView;
import android.widget.ListView;

import com.tutor.lzm.AppSession;
import com.tutor.lzm.R;
import com.tutor.lzm.activity.BaseActivity;
import com.tutor.lzm.http.response.TutorRequiresSearchBean;
import com.tutor.lzm.function.tutor.adapter.TutorHistoryAdapter;
import com.tutor.lzm.http.BaseResponse;
import com.tutor.lzm.http.HttpClient;
import com.tutor.lzm.http.request.TutorPublishHistoryRequest;
import com.tutor.lzm.http.response.TutorRequiresSearchList;
import com.tutor.syslib.Logs;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zzgpc on 2017-10-11.
 * 发布家教历史
 */
public class TutorHistoryAct extends BaseActivity {

    ListView listView = null;
    private TutorHistoryAdapter adapter;
    private List<TutorRequiresSearchBean> teacherList = new ArrayList<>();
    /**
     * 加载位置坐标
     */
    private int currentPage = 0;

    public static void start(Context context) {
        Intent intent = new Intent(context, TutorHistoryAct.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_listview);
        initTitle("我的发布");
        initListView();
        getCollectionTeacherList(true);
    }

    /**
     * 初始化
     */
    private void initListView() {
        listView = (ListView) findViewById(R.id.listView);
        listView.setHeaderDividersEnabled(false);
        listView.setDividerHeight(0);
        adapter = new TutorHistoryAdapter(this, teacherList);
        listView.setAdapter(adapter);
        ViewStub mViewStub = (ViewStub) findViewById(R.id.emptyView);
        listView.setEmptyView(mViewStub);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TutorRequiresSearchBean bean = teacherList.get(position);
                TutorDetailAct.start(oThis, bean.searchId);
            }
        });
    }


    /**
     * 我的发布
     *
     * @param isClear
     */
    private void getCollectionTeacherList(final boolean isClear) {

        if (isClear) {
            currentPage = 0;
        }

        TutorPublishHistoryRequest request = new TutorPublishHistoryRequest();
        request.userId = AppSession.getInstance().getUserId();
        request.teacherId = AppSession.getInstance().getUserId();
        request.start = currentPage;
        request.limit = 20;
        HttpClient.post(request, new HttpClient.onSuccessListener() {
            @Override
            public void onSuccess(BaseResponse response) {

                Logs.e("mainfragment", "response");
                TutorRequiresSearchList teacherListResponse = (TutorRequiresSearchList) response;
                List<TutorRequiresSearchBean> adResponse = teacherListResponse.publishSearchList;

                if (adResponse != null && adResponse.size() != 0) {
                    if (isClear) {
                        teacherList.clear();
                    }
                    currentPage++;
                    teacherList.addAll(adResponse);
                    adapter.notifyDataSetChanged();

                }
                return;

            }
        }, new HttpClient.onFailListener() {
            @Override
            public void onFail(int code, String msg) {
                Logs.d(tag, "getRecommendSubject    code" + code + "      msg=" + msg);

            }
        });

    }

}
